import { observable, action, toJS } from 'mobx';
import { Modal, Tools, Bubble } from 'weaver-mobile';
import { ListViewPage } from 'weaver-mobile-page';
import * as Api from '../../api/CommunicationApi';

const alert = Modal.alert;
const getLabel = Tools.getLabel;

const initalState = {
  coworkId: '',
  loading: true,
}
export default class QuitedListStore {
  quitList;
  @observable coworkId = initalState.coworkId;
  @observable loading = initalState.loading;

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }

  inital = (path) => {
    this.loading = true;
    this.quitList = new ListViewPage.Store({ route: path, id: 'quit' }); // 已退出人员
    this.getQuitList({coworkId: this.coworkId});
  }

  @action getQuitList = (params) => {
    Api.coworkQuitList(params).then(res => {
      if (res.status === '1') {
        const newState = {
          isTable: false,
          loading: false,
          haveCheck: true,
          template: {default: []},
          hasMore: false,
          data: [...res.shareList],
        }
        this.quitList.setState(newState);
        this.quitList.updateDataSource();
        this.loading = false;
      }
    })
  }

  // 全选
  @action doQuitSelectAll = () => this.quitList.selectAll();

  // 取消全选
  @action doQuitSelectNone = () => this.quitList.selectNone();

  // 重新加入协作
  @action reJoinCowork = () => {
    let quiterIds = toJS(this.quitList.selectedRowKeys).join();
    alert(getLabel('513246','确定要将选中人员重新加入协作吗？') , '',  [
      { text: getLabel(31129, '取消') },
      { text: getLabel(33703, '确认'),
        onPress: () => {
          let params = {coworkId: this.coworkId, quiterIds};
          Api.reJoinCoworkQuiter(params).then(res => {
            if (res.flag) {
              Bubble.success(getLabel('513249','重新加入协作成功！') );
              // this.quitList.clearData();
              this.doQuitSelectNone();
              this.getQuitList({coworkId: this.coworkId});
            } else {
              Bubble.error(getLabel('384402','操作失败，请重试！') )
            }
          })
        }
      },
    ])
  }
}